import apiClient from '../apiClient';

import { Organization } from '@/types/entity';
import {
  CreateUserReq,
  GetGroupTreeList,
  GetOrgGroupUserInfoRes,
  GetOrgGroupUserInfoReq,
  CreateGroupReq,
  UpdateGroupReq,
  GetRoleRes,
  DeleteUserReq,
  UpdateUserPasswordReq,
  UpdateUserInfo,
  ReloRevokeReq,
  ReloAssignReq,
  GetPoliciesRes,
  UpdateRoleReq,
  GetDatasetRes,
  GetDatasetReq,
  SetPoliciesReq,
  SetPoliciesRes,
  PoliciesRevokeReq,
  setPoliciesOneReq,
  GetRolesBasicReq,
  GetRolesUserReq,
  GetRolesUserRes,
  GetPoliciesPageRes,
  GetTargetTreeReq,
  GetTargetTreeRes,
  SetPoliciesTargetReq,
  GetDatasetTreeReq,
  GetDatasetTreeRes,
  GetResourceReq,
  GetDatasetNl2sqlRes,
  DatasetTypeEnum,
} from '@/common/types';

export enum OrgApi {
  Org = '/org',
  // 获取分组树形列表/创建分组树形列表
  Groups = '/openapi/v2/groups',
  // 获取分组用户信息
  GroupsUserInfo = '/openapi/v2/users',
  Role = '/openapi/v2/roles',
  RoleRevoke = '/openapi/v2/roles/revoke',
  RoleAssign = '/openapi/v2/roles/assign',
  UpdateUserState = '/openapi/v2/users/status',
  UpdateUserPassword = '/openapi/v2/users/password',
  GetPolicies = '/openapi/v2/policies',
  GetDataset = '/openapi/v2/dataset/tree',
  GetDatasetNl2sql = '/openapi/v2/dataset/database',
  SetPolicies = '/openapi/v2/policies/authz',
  PoliciesRevoke = '/openapi/v2/policies/revoke',
  GetRolesBasic = '/openapi/v2/roles/basic',
  GetRolesUser = '/openapi/v2/roles/target_users',
  GetPoliciesPage = '/openapi/v2/policies/page',
  GetTargetTree = '/openapi/v2/policies/target/tree',
  SetPoliciesTarget = '/openapi/v2/policies/target/authz',
  GetDatasetTree = '/openapi/v2/dataset/tree',
  GetResource = '/openapi/v2/policies/resource/authz',
}

const getOrgList = () => apiClient.get<Organization[]>({ url: OrgApi.Org });
const getOrgGroupTreeList = (searchText?: string) =>
  apiClient.get<GetGroupTreeList[]>({ url: OrgApi.Groups, params: { searchText: searchText } });

const createOrgGroup = (data: CreateGroupReq) =>
  apiClient.post<GetGroupTreeList>({ url: OrgApi.Groups, data });
const updateOrgGroup = (data: UpdateGroupReq) => {
  return apiClient.put<GetGroupTreeList>({ url: OrgApi.Groups + `/${data.id}`, data });
};
const deleteOrgGroup = (id: string) =>
  apiClient.delete<GetGroupTreeList>({ url: OrgApi.Groups + `/${id}` });

const getUser = (params: GetOrgGroupUserInfoReq) =>
  apiClient.get<GetOrgGroupUserInfoRes>({ url: OrgApi.GroupsUserInfo, params });

const getRole = (app_id?: string) =>
  apiClient.get<GetRoleRes[]>({ url: `${OrgApi.Role}`, params: app_id ? { app_id } : undefined });

const assignRole = (data: ReloAssignReq) => apiClient.post({ url: OrgApi.RoleAssign, data });

const createUser = (data: CreateUserReq) =>
  apiClient.post<CreateUserReq>({ url: OrgApi.GroupsUserInfo, data });

const updateUser = (data: UpdateUserInfo, id: string) =>
  apiClient.put({ url: OrgApi.GroupsUserInfo + `/${id}`, data });

const deleteUser = (data: DeleteUserReq) =>
  apiClient.delete<string>({ url: OrgApi.GroupsUserInfo, data });

const updateUserState = (id: string) =>
  apiClient.put<null>({ url: `${OrgApi.UpdateUserState}/${id}` });

const updateUserPassword = (data: UpdateUserPasswordReq) => {
  return apiClient.put<any>({
    url: OrgApi.UpdateUserPassword + `/${data.id}`,
    data: { password: data.password },
  });
};

const roleRevoke = (data: ReloRevokeReq) => apiClient.post({ url: OrgApi.RoleRevoke, data });

const getPolicies = (params: { app_id: string }) =>
  apiClient.get<GetPoliciesRes[]>({ url: OrgApi.GetPolicies, params });

const updateRole = (id: string, data: UpdateRoleReq) =>
  apiClient.put({ url: OrgApi.Role + `/${id}`, data });

const getDataset = (params: GetDatasetReq) =>
  apiClient.get<GetDatasetRes>({ url: OrgApi.GetDataset, params });

const getDatasetNl2sql = (params: GetDatasetReq) =>
  apiClient.get<GetDatasetNl2sqlRes>({
    url: OrgApi.GetDatasetNl2sql,
    params: { ...params, type: DatasetTypeEnum.database },
  });

const setPolicies = (data: SetPoliciesReq) => apiClient.post({ url: OrgApi.SetPolicies, data });

const setPoliciesOne = (data: setPoliciesOneReq) =>
  apiClient.put({ url: OrgApi.SetPolicies, data });

const policiesRevoke = (data: PoliciesRevokeReq) =>
  apiClient.post({ url: OrgApi.PoliciesRevoke, data });

const getRolesBasic = (app_id: string) =>
  apiClient.get<GetRolesBasicReq>({ url: OrgApi.GetRolesBasic, params: { app_id } });

const getRolesUser = (params: GetRolesUserReq) =>
  apiClient.get<GetRolesUserRes>({ url: OrgApi.GetRolesUser, params });

const getPoliciesPage = (params: GetPoliciesPageRes) =>
  apiClient.get({ url: OrgApi.GetPoliciesPage, params });

const getTargetTree = (data: GetTargetTreeReq) =>
  apiClient.post<GetTargetTreeRes>({ url: OrgApi.GetTargetTree, data });

const setPoliciesTarget = (data: SetPoliciesTargetReq) =>
  apiClient.post({ url: OrgApi.SetPoliciesTarget, data });

const getDatasetTree = (params: GetDatasetTreeReq) =>
  apiClient.get<GetDatasetTreeRes>({ url: OrgApi.GetDatasetTree, params });

const getResource = (data: GetResourceReq) => apiClient.post({ url: OrgApi.GetResource, data });

export default {
  getOrgList,
  getOrgGroupTreeList,
  getUser,
  createUser,
  createOrgGroup,
  updateOrgGroup,
  deleteOrgGroup,
  getRole,
  updateUser,
  deleteUser,
  updateUserState,
  updateUserPassword,
  roleRevoke,
  getPolicies,
  assignRole,
  updateRole,
  getDataset,
  setPolicies,
  policiesRevoke,
  setPoliciesOne,
  getRolesBasic,
  getRolesUser,
  getPoliciesPage,
  getTargetTree,
  setPoliciesTarget,
  getDatasetTree,
  getResource,
  getDatasetNl2sql,
};
